<template>
	<van-row class="center">
			<van-col span="12"><van-button type="default" size="small" @click="search">销量排序</van-button></van-col>
			<van-col span="12"><van-button type="default" size="small" @click="search">价格排序</van-button></van-col>
		</van-row>
	<van-card v-for="item,index in data" :key="index"
	      :price="item.price"
	      :title="item.title"
	      :thumb="item.imgUrl"
	    >
		<template #title>
			<router-link 
			:to="{'name':'commodity_detail','params':{'id':item.id}}">
				<p>{{item.title}}</p>
			</router-link>
		</template>
	      <template #desc>
	        <span style="color: gray;">{{item.delivery}}</span><br/>
	        <span style="color: gray;">其他信息</span><br/>
	      </template>
	      <template #num>
	        <van-stepper theme="round" v-model="store.cart.data[item.id]" button-size="22" min=0 
			disable-input  @change="onChange" default-value=0 />
	      </template>
	  </van-card>
	
	
	  <van-action-bar>
	    <van-action-bar-icon icon="cart-o" text="购物车" :to="{'name':'cart'}" :badge="store.cart.sum"/>
		<van-action-bar-button type="danger" text="立即下单" :to="{'name':'order_submit'}" />
	  </van-action-bar>
</template>

<script lang="ts" setup>
	import {defineProps, toRefs,ref } from 'vue';
	import {useCommonStore} from '@/store/common.ts'
	
	const store = useCommonStore()
	const props = defineProps({
		// 父组件传过来的数据
		data: Array
	})
	
  // 解构数据
  var {data} = toRefs(props)
  
  function onChange(value){
	  // 将store.cart.sum => 重新求和
	  	  console.log(value, store.cart.sum)
	  	  store.cart.sum =  0
	  	  for (var key in store.cart.data) { 
	  	      store.cart.sum = Number(store.cart.sum)+Number(store.cart.data[key])
	  	  }
	  	  console.log(store.cart.sum)
  }
  
  function search(){
	  console.log("按销量或价格排序")
	    // 这里是搜索结果 
	  	  const obj = [{
	          imgUrl:
	            '//img10.360buyimg.com/n2/s240x240_jfs/t1/210890/22/4728/163829/6163a590Eb7c6f4b5/6390526d49791cb9.jpg!q70.jpg',
	          title: '活蟹】湖塘煙雨 阳澄湖大闸蟹公4.5两 母3.5两 4对8只 鲜活生鲜螃蟹现货水产礼盒海鲜水',
	          price: '388',
	          vipPrice: '378',
	          shopDesc: '自营',
	          delivery: '厂商配送',
	        },{
	  	    id: 6,
	          imgUrl:
	            '//img10.360buyimg.com/n2/s240x240_jfs/t1/210890/22/4728/163829/6163a590Eb7c6f4b5/6390526d49791cb9.jpg!q70.jpg',
	          title: '活蟹】湖塘煙雨 阳澄湖大闸蟹公4.5两 母3.5两 4对8只 鲜活生鲜螃蟹现货水产礼盒海鲜水',
	          price: '388',
	          vipPrice: '378',
	          shopDesc: '自营',
	          delivery: '厂商配送',
	        }]
			
			data.value.length=0
			obj.forEach((item)=>{
				data.value.push(item)
			})
  }
</script>

<style>
</style>